//
//  UITabBar+SYBadge.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/27.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "UITabBar+SYBadge.h"


#define TabbarItemNums 5   //tabbar的数量

@implementation UITabBar (SYBadge)

-(void)showBadgeAtItemIndex:(int)index
{
    [self layoutSubviews];
    
    
    [self removeBadgeWithIndex:index];
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.layer.cornerRadius = 4;
    bgView.clipsToBounds = YES;
    
    bgView.tag = index + 888;
    
    
    CGRect tabFrame = self.frame;
    
    float percentX = (index +0.57) / TabbarItemNums;
    CGFloat x = ceilf(percentX * tabFrame.size.width);
    
    CGFloat y;
    
    if (isPhoneX) {
        y = ceilf(0.05 *tabFrame.size.height);
    }else
    {
        y = ceilf(0.1 * tabFrame.size.height);
    }
    
    
    bgView.frame = CGRectMake(x, y, 8, 8);
    
    [self addSubview:bgView];
    
}


-(void)hideBadgeAtItemIndex:(int)index
{
    [self removeBadgeWithIndex:index];
}

-(void)removeBadgeWithIndex:(int)index
{
    for (UIView *subview in self.subviews) {
        if (subview.tag == index + 888) {
            [subview removeFromSuperview];
        }
    }
    
}

@end
